package com.hxzy.controller.tzxm;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hxzy.common.annotation.Log;
import com.hxzy.common.core.controller.BaseController;
import com.hxzy.common.core.domain.R;
import com.hxzy.common.core.domain.entity.SysDictData;
import com.hxzy.common.core.domain.entity.SysRole;
import com.hxzy.common.enums.AckCode;
import com.hxzy.common.enums.BusinessType;
import com.hxzy.system.domain.dto.SysRoleSearchDTO;
import com.hxzy.system.domain.dto.ValueExistsDTO;
import com.hxzy.tzxm.domain.CmsCategory;
import com.hxzy.tzxm.domain.CmsContent;
import com.hxzy.tzxm.domain.dto.CmsContentSearchDTO;
import com.hxzy.tzxm.domain.vo.CmsContentVO;
import com.hxzy.tzxm.service.CmsContentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Api(tags = "文章API")
@RestController
@RequestMapping(value = "/api/cms/content")
public class CmsContentController extends BaseController {

    @Autowired
    private CmsContentService cmsContentService;

    @PreAuthorize(value = "@hxzy.hasPermi('cms:content:list')")
    @ApiOperation(value = "分页查询")
    @GetMapping(value = "/list")
    public R search(CmsContentSearchDTO cmsContentSearchDTO){
        IPage<CmsContent> page=this.cmsContentService.search(cmsContentSearchDTO);
        return super.pageVO(page);
    }

    @ApiOperation(value = "查询文章内容")
    @PreAuthorize("@hxzy.hasPermi('cms:content:query')")
    @GetMapping(value = "/{id}")
    public R getInfo(@PathVariable Long id)
    {
        CmsContentVO data=this.cmsContentService.findById(id);
        return R.okHasData(data);
    }

    @ApiOperation(value = "新增文章")
    @PreAuthorize("@hxzy.hasPermi('cms:content:add')")
    @Log(title = "文章管理", businessType = BusinessType.INSERT)
    @PostMapping
    public R add(@Validated @RequestBody CmsContentVO cmsContentVO) {
        cmsContentVO.setDelFlag("0");
        return toAjax(this.cmsContentService.add(cmsContentVO));
    }

    @ApiOperation(value = "修改文章")
    @PreAuthorize("@hxzy.hasPermi('cms:content:edit')")
    @Log(title = "文章管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public R update(@Validated @RequestBody  CmsContentVO cmsContentVO) {
        return toAjax(this.cmsContentService.edit(cmsContentVO));
    }


    @ApiOperation(value = "删除文章")
    @PreAuthorize("@hxzy.hasPermi('cms:content:remove')")
    @Log(title = "文章管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public R remove(@PathVariable Long id)
    {
        boolean result= this.cmsContentService.deleteById(id);
        return super.toAjax(result);
    }

}
